package com.my.service;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.my.domain.MyTranslucency;
import com.my.mapper.MyTranslucencyMapper;
import com.my.vo.TranslucencyVo;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class MyTranslucencyService extends ServiceImpl<MyTranslucencyMapper, MyTranslucency> {

    public List<List<TranslucencyVo>> translucencyList(String id,String name) {
        List<List<TranslucencyVo>> translucencyList = new ArrayList<>();
        List<TranslucencyVo> myTranslucencyList = baseMapper.translucencyList(new QueryWrapper<>(MyTranslucency.class)
                .eq(id != null, "mt.paper_id", id)
                .eq(name != null, "mp.name", name));
        if (CollUtil.isEmpty(myTranslucencyList)) {
            return translucencyList;
        }
        Map<Integer, List<TranslucencyVo>> groupedByTestNo = myTranslucencyList.stream()
                .collect(Collectors.groupingBy(
                        TranslucencyVo::getNo,       // 分组依据
                        Collectors.toList()              // 收集为List
                ));

        groupedByTestNo.forEach((k, v) ->
                translucencyList.add(v));
        return translucencyList;
    }

    public void paperTranslucencyDeleted(String id) {
        removeById(id);
    }

    public void translucencyEdit(MyTranslucency myTranslucency) {
        saveOrUpdate(myTranslucency);
    }
}
